<?php
Prado::using('Application.models.usuarios');
Prado::using('System.Web.UI.ActiveControls.*');

class LoginPortlet extends TTemplateControl
{

  private function sqlmap_Usuarios()
  {
      return $this->Application->getModule('SQLMap_USUARIOS')->getClient();
  }
  		    
	public function validateUser($sender,$param)
	{
    	 
	  $authManager=$this->Application->getModule('auth');
		
      $codusuario = $this->Username->Text;
      
      $new_user = new usuarios();
      $new_user->usuario = $this->Username->Text;
      $new_user->clave = $this->Password->Text;
      
      $sqlmapVal = $this->sqlmap_Usuarios();
      
      $count = $sqlmapVal->queryForObject("SelectCountUsuario",$new_user);
                
      if ($count === 0)
      {
        $param->IsValid=false;
        $this->Validador->Text = 'Usuario o clave incorrecta';
      }
      else
        {
        
        	if(!$authManager->login('demo','demo'))
			     $param->IsValid=false;
			    $this->User->Name = $this->Username->Text;			    
          $param->IsValid=true;
          $new_usuario = $sqlmapVal->queryForObject("SelectUsuario",$this->Username->Text);          
          $_SESSION['ID_USUARIO']     = $this->Username->Text;
          $_SESSION['NOMBRE_USUARIO'] = $new_usuario->nombre;         
          $this->Response->redirect('index.php?page=bienvenido');
        }
              
	}

	public function loginButtonClicked($sender,$param)
	{
    
		if($this->Page->IsValid)
			$this->Response->redirect($this->Application->getModule('auth')->getReturnUrl());
    			
	}
}

?>
